/*
 * Created on Jan 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.ui;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public abstract class PopupHandler {
    protected static final String SEPARATOR = "---";
    protected static final int MAX_SUBMENU_SIZE = 15;
    protected List<PopupData> options;
    protected Menu popupMenu;
    protected Decorations parent;
    protected Object target;
    protected SelectionListener listener;

    protected PopupHandler(Decorations parent) {
        this.parent = parent;
        DisposeListener dl = new DisposeListener() {
            public void widgetDisposed(DisposeEvent event) {
                if(popupMenu != null) popupMenu.dispose();
            }
        };
        parent.addDisposeListener(dl);

    }

    protected abstract void regenerateOptions();

    protected abstract void widgetSelected(SelectionEvent event);

    protected void widgetDefaultSelected(SelectionEvent event) {
    	//do nothing
    }

    protected void initUI() {
        popupMenu = new Menu(parent, SWT.POP_UP);
        parent.setMenu(popupMenu);
        listener = new SelectionListener() {
            public void widgetSelected(SelectionEvent event) {
                PopupHandler.this.widgetSelected(event);
            }

            public void widgetDefaultSelected(SelectionEvent event) {
                PopupHandler.this.widgetDefaultSelected(event);
            }
        };
        
        for (PopupData nvp : options) {
            MenuItem next;
            if (nvp.hasChildren()) {
                next = new MenuItem(popupMenu, SWT.CASCADE);
                Menu parent = new Menu(next);
                next.setMenu(parent);
                newItem(nvp, next);
                addChildren(parent, nvp);
            } else {
                if (nvp.equals(PopupData.SEPARATOR)) {
                    next = new MenuItem(popupMenu, SWT.SEPARATOR);
                } else {
                    next = new MenuItem(popupMenu, nvp.getType());
                    newItem(nvp, next);
                }
            }
        }
    }

    private void addChildren(Menu parent, PopupData popup) {

        if (popup.hasChildren()) {
            List<PopupData> children = popup.getChildren();
            for(PopupData child : children){
                if (child.hasChildren()) {
                    MenuItem item = new MenuItem(parent, SWT.CASCADE);
                    newItem(child, item);
                    Menu subMenu = new Menu(item);
                    item.setMenu(subMenu);
                    addChildren(subMenu, child);
                } else {

                    MenuItem item;
                    if (child.equals(PopupData.SEPARATOR)) {
                        item = new MenuItem(parent, SWT.SEPARATOR);
                    } else {
                        item = new MenuItem(parent, child.getType());
                    }
                    newItem(child, item);
                }

            }
        }

    }

    private void newItem(PopupData data, MenuItem item) {
        item.setData(data.getValue());
        item.setText(data.getText());

        item.setEnabled(data.isEnabled());
        item.setSelection(data.isChecked());
        item.addSelectionListener(listener);
    }

    protected void show() {
        regenerateOptions();
        initUI();

        popupMenu.setVisible(true);
    }

}